package com.hankol.material.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import androidx.core.content.ContextCompat;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hankol.material.R;
import com.hankol.material.bean.FolderListBean;

import java.util.List;


public class MaterialManageFolderAdapter extends BaseQuickAdapter<FolderListBean, BaseViewHolder> {
    public MaterialManageFolderAdapter(List<FolderListBean> data) {
        super(R.layout.item_material_manage_folder, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, FolderListBean item) {
        helper.setVisible(R.id.view_divider, helper.getAdapterPosition() == mData.size() - 1 ? false : true);
        helper.setText(R.id.tv_folder_des, "内容 (" + item.getFileCount() + ")");
        helper.addOnClickListener(R.id.tv_rename_folder);
        helper.addOnClickListener(R.id.tv_delete_folder);
        if (item.isEdit()) {
            helper.setText(R.id.tv_folder_name_length, item.getName().length() + "/10");
            helper.setText(R.id.tv_rename_folder, "完成");
            helper.setTextColor(R.id.tv_rename_folder, ContextCompat.getColor(mContext, R.color.color_999999));
            helper.setVisible(R.id.rl_edit, true);
            helper.setVisible(R.id.tv_delete_folder, false);
            helper.setVisible(R.id.tv_folder_name, false);
            EditText etvFolderName = helper.getView(R.id.etv_folder_name);
            etvFolderName.setText(item.getName());
            etvFolderName.setSelection(item.getName().length());
            if (item.getName().length() > 0) {
                helper.setTextColor(R.id.tv_rename_folder, ContextCompat.getColor(mContext, R.color.color_F54D53));
                item.setEnable(true);
            } else {
                helper.setTextColor(R.id.tv_rename_folder, ContextCompat.getColor(mContext, R.color.color_999999));
                item.setEnable(false);
            }
            etvFolderName.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    helper.setText(R.id.tv_folder_name_length, s.toString().length() + "/10");
                    if (s.toString().length() == 0) {
                        helper.setTextColor(R.id.tv_rename_folder, ContextCompat.getColor(mContext, R.color.color_999999));
                        item.setEnable(false);
                    } else {
                        helper.setTextColor(R.id.tv_rename_folder, ContextCompat.getColor(mContext, R.color.color_F54D53));
                        item.setEnable(true);
                        item.setName(s.toString());
                    }
                }
            });
        } else {
            helper.setText(R.id.tv_rename_folder, "重命名");
            helper.setVisible(R.id.rl_edit, false);
            helper.setVisible(R.id.tv_delete_folder, true);
            helper.setVisible(R.id.tv_folder_name, true);
            helper.setText(R.id.tv_folder_name, item.getName());
        }
    }
}
